package vip.xiaonuo.supplier.modular.custom.custom.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.xiaonuo.auth.core.util.StpLoginUserUtil;
import vip.xiaonuo.common.enums.CommonSortOrderEnum;
import vip.xiaonuo.common.exception.CommonException;
import vip.xiaonuo.common.page.CommonPageRequest;
import vip.xiaonuo.supplier.modular.custom.custom.entity.SupplierCustom;
import vip.xiaonuo.supplier.modular.custom.custom.mapper.SupplierCustomMapper;
import vip.xiaonuo.supplier.modular.custom.custom.param.SupplierCustomAddParam;
import vip.xiaonuo.supplier.modular.custom.custom.param.SupplierCustomEditParam;
import vip.xiaonuo.supplier.modular.custom.custom.param.SupplierCustomIdParam;
import vip.xiaonuo.supplier.modular.custom.custom.param.SupplierCustomPageParam;
import vip.xiaonuo.supplier.modular.custom.custom.service.SupplierCustomService;

import java.util.List;

/**
 * 客户信息Service接口实现类
 *
 * @author Mr.wu
 * @date  2025/03/17 11:20
 **/
@Service
public class SupplierCustomServiceImpl extends ServiceImpl<SupplierCustomMapper, SupplierCustom> implements SupplierCustomService {

    @Override
    public Page<SupplierCustom> page(SupplierCustomPageParam supplierCustomPageParam) {
        QueryWrapper<SupplierCustom> queryWrapper = new QueryWrapper<SupplierCustom>().checkSqlInjection();
        if(ObjectUtil.isNotEmpty(supplierCustomPageParam.getCustomName())) {
            queryWrapper.lambda().like(SupplierCustom::getCustomName, supplierCustomPageParam.getCustomName());
        }
        if(ObjectUtil.isNotEmpty(supplierCustomPageParam.getCustomCustomType())) {
            queryWrapper.lambda().eq(SupplierCustom::getCustomCustomType, supplierCustomPageParam.getCustomCustomType());
        }
        if(ObjectUtil.isNotEmpty(supplierCustomPageParam.getCustomTel())) {
            queryWrapper.lambda().like(SupplierCustom::getCustomTel, supplierCustomPageParam.getCustomTel());
        }
        if(ObjectUtil.isNotEmpty(supplierCustomPageParam.getCustomAddress())) {
            queryWrapper.lambda().like(SupplierCustom::getCustomAddress, supplierCustomPageParam.getCustomAddress());
        }
        if(ObjectUtil.isNotEmpty(supplierCustomPageParam.getCustomMobile())) {
            queryWrapper.lambda().like(SupplierCustom::getCustomMobile, supplierCustomPageParam.getCustomMobile());
        }
        if(ObjectUtil.isNotEmpty(supplierCustomPageParam.getCustomStore())) {
            queryWrapper.lambda().eq(SupplierCustom::getCustomStore, supplierCustomPageParam.getCustomStore());
        }
        if(ObjectUtil.isNotEmpty(supplierCustomPageParam.getCustomTag())) {
            queryWrapper.lambda().eq(SupplierCustom::getCustomTag, supplierCustomPageParam.getCustomTag());
        }
        if(ObjectUtil.isNotEmpty(supplierCustomPageParam.getCustomGender())) {
            queryWrapper.lambda().eq(SupplierCustom::getCustomGender, supplierCustomPageParam.getCustomGender());
        }
        if(ObjectUtil.isNotEmpty(supplierCustomPageParam.getStartCustomBirthday()) && ObjectUtil.isNotEmpty(supplierCustomPageParam.getEndCustomBirthday())) {
            queryWrapper.lambda().between(SupplierCustom::getCustomBirthday, supplierCustomPageParam.getStartCustomBirthday(), supplierCustomPageParam.getEndCustomBirthday());
        }
        if(ObjectUtil.isNotEmpty(supplierCustomPageParam.getCustomDelivery())) {
            queryWrapper.lambda().eq(SupplierCustom::getCustomDelivery, supplierCustomPageParam.getCustomDelivery());
        }
        //数据归属
        queryWrapper.lambda().eq(SupplierCustom::getCompanyId, StpLoginUserUtil.getLoginUser().getCompanyId());
        if(ObjectUtil.isAllNotEmpty(supplierCustomPageParam.getSortField(), supplierCustomPageParam.getSortOrder())) {
            CommonSortOrderEnum.validate(supplierCustomPageParam.getSortOrder());
            queryWrapper.orderBy(true, supplierCustomPageParam.getSortOrder().equals(CommonSortOrderEnum.ASC.getValue()),
                    StrUtil.toUnderlineCase(supplierCustomPageParam.getSortField()));
        } else {
            queryWrapper.lambda().orderByAsc(SupplierCustom::getSortCode);
        }
        return this.page(CommonPageRequest.defaultPage(), queryWrapper);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(SupplierCustomAddParam supplierCustomAddParam) {
        SupplierCustom supplierCustom = BeanUtil.toBean(supplierCustomAddParam, SupplierCustom.class);
        //数据归属
        supplierCustom.setCompanyId(StpLoginUserUtil.getLoginUser().getCompanyId());
        this.save(supplierCustom);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(SupplierCustomEditParam supplierCustomEditParam) {
        SupplierCustom supplierCustom = this.queryEntity(supplierCustomEditParam.getId());
        BeanUtil.copyProperties(supplierCustomEditParam, supplierCustom);
        this.updateById(supplierCustom);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<SupplierCustomIdParam> supplierCustomIdParamList) {
        // 执行删除
        this.removeByIds(CollStreamUtil.toList(supplierCustomIdParamList, SupplierCustomIdParam::getId));
    }

    @Override
    public SupplierCustom detail(SupplierCustomIdParam supplierCustomIdParam) {
        return this.queryEntity(supplierCustomIdParam.getId());
    }

    @Override
    public SupplierCustom queryEntity(String id) {
        SupplierCustom supplierCustom = this.getById(id);
        if(ObjectUtil.isEmpty(supplierCustom)) {
            throw new CommonException("客户信息不存在，id值为：{}", id);
        }
        return supplierCustom;
    }
}
